'use strict';

angular.module('app')
	.controller('authGroupController', [ '$scope', '$rootScope', '$state', '$timeout', 'toaster',
	                                function($scope, $rootScope, $state, $timeout, toaster) {
		$scope.title = '用户组授权列表';
        $scope.param = { };
        $scope.loading = false;
        
        $scope.regionName = $state.params.regionName;
    	$scope.param.regionId = $state.params.regionId;
        
		$scope.search = function () {
	        $scope.loading = true;
	        if($scope.pageFlag != 1){//未点击翻页
			  	   $scope.param.pageNum=1;//将页码设置为1，即跳转回第一页
			  	}else{//点击翻页
			  	   $scope.pageFlag = 2;//将开关值设置为2
			  	}
			$.ajax({
				type: 'PUT',
	            dataType: 'json',
				contentType:'application/json;charset=UTF-8',
				url : '/dac/authm/group/list',
				data: angular.toJson($scope.param)
			}).then(function(result) {
		        $scope.loading = false;
				if (result.httpCode == 200) {
					$scope.pageInfo = result;
				} else {
					$scope.msg = result.msg;
				}
				$scope.$apply();
			});
		}
		//从本地获取localstorage数据，对权限按钮做隐藏
		var personAuth =localStorage.getItem("PersonAuth");//获取PersonAuth的值
		personAuth = personAuth.split(',');
		for(var i = 0;i<personAuth.length;i++){
			if(personAuth[i]=='main.authm.cardAuth.read'){
				$scope.read = true;
			}
			if(personAuth[i]=='main.authm.cardAuth.add'){
				$scope.add = true;
			}
			if(personAuth[i]=='main.authm.cardAuth.update'){
				$scope.update = true;
			}
			if(personAuth[i]=='main.authm.cardAuth.delete'){
				$scope.dele = true;
			}
		}
		$scope.search();

		$scope.doSearch = function(){
			$scope.param.pageNum=1;
			$scope.search();
		}
		
		$scope.clearSearch = function() {
			$scope.param.keyword= null;
			$scope.search();
		}
		
		// 翻页
        $scope.pagination = function (page) {
            $scope.param.pageNum=page;
            $scope.pageFlag = 1;//将开关值设置为1
            $scope.search();
        };
        
        $scope.showMsg = function(msg){
        	alert(msg);
        }
        
        $scope.delItem = function(id,gname,dname){
        	if (confirm('确认要删除设备['+dname+']对区域['+gname+']的授权吗?')){
        		$.ajax({
    				type: 'POST',
    	            dataType: 'json',
    				contentType:'application/json;charset=UTF-8',
    				url : '/dac/authm/group/del',
    				data: angular.toJson({'id':id})
    			}).then(function(result) {
    				if (result.httpCode == 200) {
    					$scope.search();
    				} else {
    					toaster.clear('*');
                        toaster.pop('error', '', result.msg);
    				}
    			});
        	} 
        }
} ]);